#include "login.h"
#include "ui_login.h"
#include <QMouseEvent>
#include <QMessageBox>
#include <QDebug>
#include "QJsonParseError"
#include "QJsonArray"
#include "QJsonDocument"
#include "QJsonObject"
#include "QJsonValueRef"

Login::Login(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Login)
{
    ui->setupUi(this);

    basicSettings();
    connectSettings();
}

Login::~Login()
{
    delete ui;
}

void Login::basicSettings()
{
    //唯一指定Person(@_@)
    login_user = new Person(this);

    //消除边框
    this->setWindowFlag(Qt::FramelessWindowHint);

    //密码框
    ui->editPassWord->setEchoMode(QLineEdit::Password);

    //轮播框
    for(int i = 1; i<=10; i++)
    {
        pixmapList.append(QPixmap(QString(":/picture/%1.png").arg(i)));
    }
    rotation = new pictureWidget(nullptr, &pixmapList);
    ui->vertRotationWidget->addWidget(rotation);

    ui->editId->setMaxLength(8);
    ui->editPassWord->setMaxLength(8);
}

void Login::connectSettings()
{
    //关闭与最小化
    connect(ui->btnClose, &QPushButton::clicked, this, &QWidget::close);
    connect(ui->btnMinimize, &QPushButton::clicked, this, &QWidget::showMinimized);

    //登录和注册
    connect(ui->btnLogin, &QPushButton::clicked, this, &Login::loginJudge);
    connect(ui->btnRegister, &QPushButton::clicked, this, &Login::btnRegister_clicked);

    //enter响应
    connect(this, &Login::keyEnter, ui->btnLogin, &QPushButton::click);
}

void Login::update()
{
    ui->editId->setText("");
    ui->editPassWord->setText("");
}

void Login::dealMsg1(QNetworkReply *reply)
{
    QByteArray array = reply->readAll();
    QJsonParseError error;
    QJsonDocument data = QJsonDocument:: fromJson(array,&error);

    if(!data.isNull())
    {
        QJsonArray obj = data.array();
        QJsonObject object = obj[0].toObject();

        login_user->setId(object.value("uid").toString());
        login_user->setPassWord(object.value("password").toString());
        login_user->setName(object.value("nickname").toString());
        login_user->setSignature(object.value("signature").toString());
        login_user->setSex(object.value("sex").toInt());
        login_user->setPhoneNumber(object.value("phone").toString());
        QString s = object.value("birthday").toString();
        //qDebug() << s;
        QDate date = QDate::fromString(s,"yyyy-MM-dd");
        login_user->setBirthday(date);
        //qDebug() << date;
        login_user->setPixmapPath(object.value("Pixmap").toString());
        login_user->setPixmap(QPixmap(login_user->getPixmap()));


        mainWindow = new MainWidget(nullptr, login_user);
        //二次登录
        connect(mainWindow, &MainWidget::backLogin, this, &Login::nextLogin);
        mainWindow->show();
        this->close();
    }

}

void Login::dealMsg(QNetworkReply *reply)
{
    QByteArray array = reply->readAll();
    QString s = array;

    if(QString::compare(s,"登录成功") == 0)
    {
        //信号发送
        // URL
        QString baseUrl1 = "http://127.0.0.1:5000/UserInfoCheck?Userid=" + ui->editId->text();
        QUrl url(baseUrl1);

        // 构造请求QNetworkRequest
        QNetworkRequest request1;
        request1.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
        request1.setUrl(url);

        QNetworkAccessManager *manager1 = new QNetworkAccessManager();

        // 发送get请求
        manager1->get(request1);

        connect(manager1,SIGNAL(finished(QNetworkReply *)),this,SLOT(dealMsg1(QNetworkReply *)));


    }
    else
    {
        QMessageBox::critical(this, "登录失败", "账号或密码错误！", QMessageBox::Ok, QMessageBox::Ok);
    }
}

void Login::loginJudge()
{
    //信号发送
    // URL
    QString baseUrl = "http://127.0.0.1:5000/Login?Userid=" + ui->editId->text() + "&Password=" + ui->editPassWord->text();
    QUrl url(baseUrl);

    // 构造请求QNetworkRequest
    QNetworkRequest request;
    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
    request.setUrl(url);

    QNetworkAccessManager *manager = new QNetworkAccessManager();

    // 发送get请求
    manager->get(request);

    connect(manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(dealMsg(QNetworkReply*)));
}

void Login::nextLogin()
{
    //释放前一个主窗口
    delete mainWindow;
    update();
    this->show();
}

void Login::mousePressEvent(QMouseEvent *event)
{
    if(event->buttons() & Qt::LeftButton)
    {
        address = event->globalPos();
    }
}
void Login::mouseMoveEvent(QMouseEvent *event)
{
    if(event->buttons() & Qt::LeftButton)
    {
        int dx = event->globalX() - address.x();
        int dy = event->globalY() - address.y();
        this->move(this->x()+dx, this->y()+dy);
        address = event->globalPos();
    }
}
void Login::mouseReleaseEvent(QMouseEvent *event)
{
    if(event->buttons() & Qt::LeftButton)
    {
        int dx = event->globalX() - address.x();
        int dy = event->globalY() - address.y();
        this->move(this->x()+dx, this->y()+dy);
        address = event->globalPos();
    }
}

void Login::keyPressEvent(QKeyEvent * event)
{
    if(event->key() == Qt::Key_Enter || event->key() == Qt::Key_Return)
    {
        emit keyEnter();
    }
}

void Login::btnRegister_clicked()
{
    if(registID == nullptr)
    {
        registID = new registerWidget();
        //设置注册窗口为模态
        registID->setWindowModality(Qt::ApplicationModal);
    }
    registID->setGeometry(615, 300, 700, 457);
    registID->raise();
    registID->show();
}
